/** 
* All Rights Reserved , Copyright (C) 2020 , 青岛鼎信通讯股份有限公司
* 
* BaseTeamUserService
* 团队人员
* 
* 修改纪录
* 2020-08-27 版本：1.0 jiangtao 创建。
* @version 版本：1.0
* @author 作者：jiangtao
* 创建日期：2020-08-27
*/
package com.topscomm.base.service.impl;

import com.topscomm.base.pojo.BaseTeamUserEntity;
import com.topscomm.basic.util.SQLAdapter;
import com.topscomm.pub.util.StringBufferProxy;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.topscomm.base.service.IBaseTeamUserService;
import com.topscomm.base.service.impl.auto.BaseTeamUserServiceAuto;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service(value = "baseTeamUserService")
@Scope(value = "singleton")
public class BaseTeamUserService extends BaseTeamUserServiceAuto implements IBaseTeamUserService {

    /**
     * @return
     * @author: shenyouyi
     * @date: 2020-08-25
     * @param: teamids
     * @description: 根据团队ID获取用户信息
     * @modify:
     */
    public List<Map<String, Object>> searchUsersByTeams(String teamids) {
        String[] deptIdArray = teamids.split(",");
        StringBufferProxy idParam = new StringBufferProxy();
        for (String dptId : deptIdArray) {
            idParam.appendSingle("{0},", dptId);
        }
        if (idParam.length() > 0) {
            idParam = idParam.substring(0, idParam.length() - 1);
        }
        List<String> queryFields = new ArrayList<>();
        queryFields.add(BaseTeamUserEntity.FieldUsercode + " as code");
        queryFields.add(BaseTeamUserEntity.FieldUsername + " as name");
        StringBufferProxy sql = new StringBufferProxy();
        sql.appendSingle(BaseTeamUserEntity.FieldTeamid + " in ({0}) and " + BaseTeamUserEntity.FieldPositionbegintime + " < {1}", idParam.toString(), SQLAdapter.getDate());
        List<Map<String, Object>> userMapList = this.queryMapFieldsByWhere(sql.toString(), queryFields);
        return userMapList;
    }

    /**
     * @author: shenyouyi
     * @date: 2020-08-31
     * @param: teamids
     * @return
     * @description: 根据团队查询是否有人员
     * @modify:
     */
    @Override
    public boolean searchHaveUsersByTeams(String teamids) {
        int userCount = this.searchUserCountByTeams(teamids);
        return userCount > 0;
    }

    @Override
    public int searchUserCountByTeams(String teamids) {
        String[] deptIdArray = teamids.split(",");
        StringBufferProxy idParam = new StringBufferProxy();
        for (String dptId : deptIdArray) {
            idParam.appendSingle("{0},", dptId);
        }
        if (idParam.length() > 0) {
            idParam = idParam.substring(0, idParam.length() - 1);
        }
        StringBufferProxy sql = new StringBufferProxy();
        sql.appendSingle(BaseTeamUserEntity.FieldTeamid + " in ({0}) and " + BaseTeamUserEntity.FieldPositionbegintime + " < {1}", idParam.toString(), SQLAdapter.getDate());
        int userCount = this.getCount(sql.toString());
        return userCount;
    }
}
